package com.xilihui.xlh.business.requests;

import android.content.Context;

import com.xilihui.xlh.business.entities.AreaEntity;
import com.xilihui.xlh.business.entities.ShareEntity;
import com.xilihui.xlh.business.entities.UploadingEntity;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.HttpManager;
import com.xilihui.xlh.core.util.CompressUtil;
import com.xilihui.xlh.core.util.SPUtil;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Streaming;
import retrofit2.http.Url;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/6/28.
 */

public class SignedRequest {

    public static SignedService service = HttpManager.getRetrofit().create(SignedService.class);
    
    /**
     * 压缩上传单张图片
     *
     * @param context
     * @param path
     * @return
     */
    public static Observable<UploadingEntity> uploadImgAndCompress(final Context context, String path) {

        return Observable.just(path)
                .subscribeOn(Schedulers.io())
                .map(new Func1<String, String>() {
                    @Override
                    public String call(String s) {
                        return CompressUtil.compressSample(context, s);
                    }
                })
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<UploadingEntity>>() {
                    @Override
                    public Observable<UploadingEntity> call(String s) {
                        RequestBody img = RequestBody.create(MediaType.parse("image/jpeg"), new File(s));
                        String access_token = (String)SPUtil.get(context,SPUtil.TOKEN,"");
                        return service.uploadImg(img,access_token);
                    }
                });
    }

    /**
     * 签约申请
     * @param context
     * @param head_pic
     * @param read_name
     * @param sex
     * @param birthday
     * @param working_age
     * @param province
     * @param city
     * @param district
     * @param idcard
     * @param idcard_img_hand
     * @param idcard_img_back
     * @param idcard_img_front
     * @return
     */
    public static Observable<BaseEntity> submit(Context context,
                                                String head_pic,
                                                String read_name,
                                                String sex,
                                                String birthday,
                                                String working_age,
                                                String province,
                                                String city,
                                                String district,
                                                String idcard,
                                                String idcard_img_hand,
                                                String idcard_img_back,
                                                String idcard_img_front){
        String access_token = (String) SPUtil.get(context,SPUtil.TOKEN,"");
        return service.submit(access_token,head_pic,read_name,sex,birthday,working_age,province,
                city,district,idcard,idcard_img_hand,idcard_img_back,idcard_img_front);
    };
    
    /**
     * 获取地址
     * @param context
     * @return
     */
    public static Observable<AreaEntity> getAddress(Context context){
        String access_token = (String) SPUtil.get(context,SPUtil.TOKEN,"");
        return service.getAddress(access_token);
    };

    /**
     * 判断是否提交申请
     * @param context
     * @return
     */
    public static Observable<BaseEntity> isApply(Context context){
        String access_token = (String)SPUtil.get(context,SPUtil.TOKEN,"");
        return service.isApply(access_token);
    }

    /**
     * 分享 二维码图片
     * @param context
     * @return
     */
    public static Observable<ShareEntity> weixinQRCode(Context context){
        String access_token = (String)SPUtil.get(context,SPUtil.TOKEN,"");
        return service.weixinQrCode(access_token);
    }


}
